import { defineStore } from 'pinia'; // 定义一个pinia的一个模块
//这里是练习pinia用的
import type { StorageLike } from 'pinia-plugin-persistedstate';
import SecureLS from 'secure-ls';
// encryptionSecret:自定义密钥
const ls = new SecureLS({
  isCompression: false,
  encryptionSecret: 'A-lone-Wolf-screams-in-the-night-sky',
});
const st: StorageLike = {
  setItem(key: string, value: string) {
    ls.set(key, value);
  },
  getItem(key: string): string | null {
    return ls.get(key);
  },
};

//'第一个参数是id标识，pinia的名字'，'第二个参数是个对象'
const useCounterStore = defineStore('counter', {
  state: () => {
    return {
      count: 10,
      msg: '前端进阶',
      price: 100,
    };
  },
  actions: {
    add() {
      this.count += 1;
    },
    del() {
      this.count -= 1;
    },
    promiseIncrement() {
      return new Promise((resolve) => {
        setTimeout(() => {
          this.count++;
          resolve(this.count);
        }, 1000);
      });
    },
    priceAdd(a: any, b: any) {
      console.log(a, b);

      this.price++;
    },
  },
  getters: {
    getCount(state) {
      //在使用时，把方法名当属性用就行
      return state.count * state.price;
    },
  },
  // persist: true, 默认存储在 localStorage上面的
  persist: {
    storage: st, // 上面申明的类型
    // storage: localStorage,
    // key: 'counter',
  },
});
export default useCounterStore;
